package com.iris.live.services.report.sources;

import com.iris.live.services.data.models.Lsh经销商表Model;
import com.iris.live.services.data.repositories.CRMRepository;
import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/3/7.
 */
@Component("ExportCRMSurveySource")
public class ExportCRMSurveySource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportCRMSurveySource.class);

    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Autowired
    private CRMRepository crmRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("调研问卷信息", "调研问卷信息", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String permit = null, username = null;
        if (context.get("permit") == null || context.get("username") == null) {
            return null;
        }
        permit = context.get("permit").toString();
        Lsh经销商表Model lsh经销商表Model = lsh经销商表Repository.getLshDistributorByPermit(permit);
        if (lsh经销商表Model == null) {
            return null;
        }
        username = context.get("username").toString();
        String stage = context.get("stage") == null ? null : context.get("stage").toString();
        String surveyResult = context.get("surveyResult") == null ? null : context.get("surveyResult").toString();
        String cusName = context.get("cusName") == null ? null : context.get("cusName").toString();
        String cusPhone = context.get("cusPhone") == null ? null : context.get("cusPhone").toString();
        String researchState = context.get("researchState") == null ? null : context.get("researchState").toString();
        String connectStartDate = context.get("connectStartDate") == null ? null : context.get("connectStartDate").toString();
        String connectEndDate = context.get("connectEndDate") == null ? null : context.get("connectEndDate").toString();
        String nowState = context.get("nowState") == null ? null : context.get("nowState").toString();
        String intentCarVehicles = context.get("intentCarVehicles") == null ? null : context.get("intentCarVehicles").toString();
        String intentCarStyle = context.get("intentCarStyle") == null ? null : context.get("intentCarStyle").toString();
        String nextConnectStartDate = context.get("nextConnectStartDate") == null ? null : context.get("nextConnectStartDate").toString();
        String nextConnectEndDate = context.get("nextConnectEndDate") == null ? null : context.get("nextConnectEndDate").toString();
        String firstToShopDateStr = context.get("firstToShopDate") == null ? "0" : context.get("firstToShopDate").toString();
        String NSPDateStr = context.get("NSPDate") == null ? "0" : context.get("NSPDate").toString();
        String pageSize = context.get("pageSize") == null ? "0" : context.get("pageSize").toString();
        String pageIndex = context.get("pageIndex") == null ? "0" : context.get("pageIndex").toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date firstToShopDate = null, NSPDate = null;
        try {
            firstToShopDate = new Date(sdf.parse(firstToShopDateStr).getTime());
            NSPDate = new Date(sdf.parse(NSPDateStr).getTime());
        } catch (Exception e) {
            logger.error("顾客首次离店回访间隔天数/CRM_NPS天数转换异常");
        }

        List values = getCRMSurvey(permit, username, firstToShopDate, NSPDate, stage, cusName, cusPhone, researchState,
                connectStartDate, connectEndDate, nowState, intentCarVehicles, intentCarStyle, nextConnectStartDate,
                nextConnectEndDate, surveyResult, Integer.parseInt(pageSize), Integer.parseInt(pageIndex));
        List<Row> list = new ArrayList<>();

        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        Cell[] cells = new Cell[2];
        cells[0] = new Cell("经销商名称：" + lsh经销商表Model.get经销商名(), 1);
        cells[1] = new Cell("经销商代码：" + lsh经销商表Model.getBmbs(), 3);
        list.add(new Row(cells));
        list.add(new Row());
        list.add(new Row());

        for (Object o : values) {
            list.add(new Row(getCells(o)));
        }

        return list;
    }

    private Cell[] getCells(Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[14];
        int index = 0;
        cells[index++] = new Cell(map.get("stageName") == null ? "" : map.get("stageName").toString(), index);
        cells[index++] = new Cell(map.get("surveyResult") == null ? "" : map.get("surveyResult").toString(), index);
        cells[index++] = new Cell(map.get("valid") == null ? "" : map.get("valid").toString(), index);
//        cells[index++] = new Cell(map.get("nowState") == null ? "" : map.get("nowState").toString(), index);
        cells[index++] = new Cell(map.get("cusName") == null ? "" : map.get("cusName").toString(), index);
        cells[index++] = new Cell(map.get("cusAppellation") == null ? "" : map.get("cusAppellation").toString(), index);
        cells[index++] = new Cell(map.get("cusPhone") == null ? "" : map.get("cusPhone").toString(), index);
        cells[index++] = new Cell(map.get("firstStoreDate") == null ? "" : map.get("firstStoreDate").toString(), index);
        cells[index++] = new Cell(map.get("orderIntervalDays") == null ? "" : map.get("orderIntervalDays").toString(), index);
        cells[index++] = new Cell(map.get("intentCarVehicles") == null ? "" : map.get("intentCarVehicles").toString(), index);
        cells[index++] = new Cell(map.get("intentCarStyle") == null ? "" : map.get("intentCarStyle").toString(), index);
        cells[index++] = new Cell(map.get("scName") == null ? "" : map.get("scName").toString(), index);
        cells[index++] = new Cell(map.get("connectDate") == null ? "" : map.get("connectDate").toString(), index);
        cells[index++] = new Cell(map.get("connectTime") == null ? "" : map.get("connectTime").toString(), index);
        cells[index++] = new Cell(map.get("nextConnectDate") == null ? "" : map.get("nextConnectDate").toString(), index);
        return cells;
    }

    private List getCRMSurvey(String permit, String username, Date firstToShopDate, Date NPSDate, String stage, String cusName,
                              String cusPhone, String researchState, String connectStartDate, String connectEndDate, String nowState,
                              String intentCarVehicles, String intentCarStyle, String nextConnectStartDate, String nextConnectEndDate,
                              String surveyResult,int pageSize, int pageIndex) {
        List list = new ArrayList();
        try {
            PageData pageData = null;

            pageData = crmRepository.findSurveyInfo(permit, username, firstToShopDate, NPSDate, stage,cusName,
                    cusPhone, researchState, connectStartDate, connectEndDate, nowState,intentCarVehicles, intentCarStyle,
                    nextConnectStartDate, nextConnectEndDate, surveyResult, pageSize, pageIndex);

            if (pageData != null) list = pageData.getData();
        } catch (Exception e) {
            logger.error("调研问卷信息导出程序异常", e);
        }
        return list;
    }

}
